<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: html.class.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
defined('IN_TWCMS') || exit('Access Denied');

class html{
	public $db;
	public $G;

	function __construct($obj) {
		set_time_limit(0);
		$this->db = &$obj->db;
		$this->G = &$obj->G;
	}

	#生成首页
	function write_index() {
		$str = get_front();
		f_write(TWCMS_ROOT.$this->G['html_index'], $str);
	}

	#生成分类页
	function write_cate($sql) {
		$query = $this->db->query($sql);
		while($row = $this->db->fetch_array($query)) {
			$_GET['u'] = $this->G['url_cate'].P.$row['cid'];
			$str = get_front();
			if(isset($GLOBALS['html_path'])) {
				$updir = TWCMS_ROOT.dirname($GLOBALS['html_path']).D;
				_mkdir($updir);
				$path = $updir.$this->G['html_index'];
				f_write($path, $str);
			}

			if(isset($GLOBALS['html_total']) && $GLOBALS['html_maxpage']>1) {
				$i = 0;
				while($i<$GLOBALS['html_maxpage']) {
					$i++;
					$_GET['u'] = $this->G['url_cate'].P.$row['cid'].P.$i;
					$str = get_front();
					if(isset($GLOBALS['html_path'])) {
						$path = TWCMS_ROOT.$GLOBALS['html_path'];
						_mkdir(dirname($path));
						f_write(str_replace('{page}', $i, $path), $str);
					}
				}
				unset($GLOBALS['html_total']);
			}
		}
	}

	#生成内容页
	function write_show($sql) {
		$query = $this->db->query($sql);
		while($row = $this->db->fetch_array($query)) {
			$_GET['u'] = $this->G['url_show'].P.$row['id'];
			$str = get_front();
			if(isset($GLOBALS['html_path']) && $GLOBALS['html_path']!=$_ENV->G['html_dir']) {
				$path = TWCMS_ROOT.$GLOBALS['html_path'];
				_mkdir(dirname($path));
				f_write($path, $str);
			}
		}
	}
}